表单控件样式

在本教程中,我们将深入探讨如何使用 Bootstrap 来设计和样式化表单控件。表单是网页中常见的元素,用于收集用户输入。通过 Bootstrap,我们可以轻松地创建美观且功能强大的表单。

1. 表单控件基础

1.1 表单控件的类型

Bootstrap 支持多种表单控件,包括:

  • 文本输入框 (<input type="text">)
  • 密码输入框 (<input type="password">)
  • 单选按钮 (<input type="radio">)
  • 复选框 (<input type="checkbox">)
  • 下拉菜单 (<select>)
  • 文本区域 (<textarea>)

1.2 基本表单结构

在 Bootstrap 中,表单控件通常包裹在一个 <form> 元素中,并使用 Bootstrap 的类来样式化。以下是一个基本的表单结构示例:

<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

1.3 表单控件的样式类

Bootstrap 提供了多种类来样式化表单控件:

  • form-control:用于文本输入框、密码输入框、文本区域等。
  • form-check:用于单选按钮和复选框。
  • form-check-input:用于单选按钮和复选框的输入部分。
  • form-check-label:用于单选按钮和复选框的标签部分。

2. 表单控件的布局

2.1 水平布局

使用 form-group 类可以创建垂直布局的表单。如果需要水平布局,可以使用 form-row 和 col 类:

<form>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputEmail4">Email</label>
      <input type="email" class="form-control" id="inputEmail4" placeholder="Email">
    </div>
    <div class="form-group col-md-6">
      <label for="inputPassword4">Password</label>
      <input type="password" class="form-control" id="inputPassword4" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <label for="inputAddress">Address</label>
    <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

2.2 内联表单

使用 form-inline 类可以创建内联表单,所有控件将水平排列:

<form class="form-inline">
  <label class="sr-only" for="inlineFormInputName2">Name</label>
  <input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">

  <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
  <div class="input-group mb-2 mr-sm-2">
    <div class="input-group-prepend">
      <div class="input-group-text">@</div>
    </div>
    <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
  </div>

  <button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>

3. 表单控件的验证状态

Bootstrap 提供了多种验证状态的样式,帮助用户了解输入是否有效:

  • is-valid:表示输入有效。
  • is-invalid:表示输入无效。
<form>
  <div class="form-group">
    <label for="validationServer01">First name</label>
    <input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
    <div class="valid-feedback">
      Looks good!
    </div>
  </div>
  <div class="form-group">
    <label for="validationServer02">Last name</label>
    <input type="text" class="form-control is-invalid" id="validationServer02" value="Otto" required>
    <div class="invalid-feedback">
      Please provide a valid last name.
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

4. 实践练习

4.1 创建一个注册表单

使用 Bootstrap 创建一个注册表单,包含以下字段:

  • 用户名
  • 电子邮件
  • 密码
  • 确认密码
  • 同意条款的复选框

4.2 代码示例

<form>
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control" id="username" placeholder="Enter username">
  </div>
  <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Enter email">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="confirmPassword">Confirm Password</label>
    <input type="password" class="form-control" id="confirmPassword" placeholder="Confirm Password">
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input" id="agreeTerms">
    <label class="form-check-label" for="agreeTerms">I agree to the terms and conditions</label>
  </div>
  <button type="submit" class="btn btn-primary">Register</button>
</form>

4.3 添加验证状态

为表单添加验证状态,当用户输入无效时显示错误信息。

<form>
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control is-invalid" id="username" placeholder="Enter username">
    <div class="invalid-feedback">
      Please choose a username.
    </div>
  </div>
  <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control is-invalid" id="email" placeholder="Enter email">
    <div class="invalid-feedback">
      Please provide a valid email.
    </div>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control is-invalid" id="password" placeholder="Password">
    <div class="invalid-feedback">
      Password must be at least 8 characters.
    </div>
  </div>
  <div class="form-group">
    <label for="confirmPassword">Confirm Password</label>
    <input type="password" class="form-control is-invalid" id="confirmPassword" placeholder="Confirm Password">
    <div class="invalid-feedback">
      Passwords do not match.
    </div>
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input is-invalid" id="agreeTerms">
    <label class="form-check-label" for="agreeTerms">I agree to the terms and conditions</label>
    <div class="invalid-feedback">
      You must agree before submitting.
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Register</button>
</form>

5. 总结

通过本教程,您已经学习了如何使用 Bootstrap 来样式化和布局表单控件。您还了解了如何添加验证状态以提高用户体验。继续实践和探索,您将能够创建更加复杂和功能强大的表单。

6. 下一步

接下来,您可以学习如何使用 Bootstrap 的表单验证插件,或者探索如何自定义表单控件的样式。